package com.egreen.asset.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egreen.asset.dao.PointAccountDao;
import com.egreen.asset.service.IPointAccountService;
import com.egreen.common.pojo.entity.asset.PointAccount;
import com.egreen.common.serviceapi.user.IUserServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Service
public class PointAccountServiceImpl extends ServiceImpl<PointAccountDao, PointAccount> implements IPointAccountService {
    @Autowired
    PointAccountDao pointAccountDao;
    @DubboReference
    IUserServiceApi userServiceApi;

    /**
     * 新建积分账户
     *
     * @return
     */
    @Override
    public Long addPointAccount() {
        PointAccount pointAccount = new PointAccount();
        //传入新建账户的余额
        pointAccount.setPointBalance(0);
        //将账户保存回pointAccount
        pointAccountDao.insert(pointAccount);
        //返回值即为id，直接获取id即可
        return pointAccount.getId();
    }

    /**
     * 通过 userId查询用户积分
     *
     * @param userId
     * @return
     */
    @Override
    public Integer selectPointByAccountId(Long userId) {
        Long pointAccountId = userServiceApi.selectUserById(userId).getPointAccountId();
        PointAccount pointAccount = pointAccountDao.selectById(pointAccountId);
        Integer Point = pointAccount.getPointBalance();
        return Point;
    }

}
